package com.albermark.photoorganizer.pojo;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;

/**
 * 
 * @author Nuno Ferreira
 *
 */
public class Photo {

	private final File file;
	private Calendar fileOriginalDateCalendar;
	private String fileExtension;
	
	public Photo(File file) throws ImageProcessingException, ParseException, IOException {
		this.file = file;
		
		initialize();
	}
	
	private void initialize() throws ImageProcessingException, ParseException, IOException {

		this.fileOriginalDateCalendar = extractOriginalTime();

		this.fileExtension = extractFileExtension();
	}
	
	private Calendar extractOriginalTime() throws ParseException, ImageProcessingException, IOException{
		DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
		
		Metadata metadata = ImageMetadataReader.readMetadata(file);
		
		ExifSubIFDDirectory directory = metadata.getDirectory(ExifSubIFDDirectory.class);
		
		String originalDateStr = directory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
		
		Calendar photoDateCalendar = Calendar.getInstance();
		photoDateCalendar.setTime(dateFormat.parse(originalDateStr));
		
		return photoDateCalendar;
	}
	
	private String extractFileExtension() {
		int separatorIndex = file.getName().lastIndexOf(".");
		
		if (separatorIndex >= 0 ) {
			return file.getName().substring(separatorIndex);
		}
		
		return null;
	}
	
	public String getBaseFileName() {
		StringBuilder dirNameBuilder = new StringBuilder();
		
		dirNameBuilder.append(fileOriginalDateCalendar.get(Calendar.YEAR));
		dirNameBuilder.append("-");
		int month = fileOriginalDateCalendar.get(Calendar.MONTH) + 1;
		dirNameBuilder.append(month < 10? "0" + month: month);
		dirNameBuilder.append("-");
		int day = fileOriginalDateCalendar.get(Calendar.DAY_OF_MONTH);
		dirNameBuilder.append(day < 10? "0" + day: day);
		dirNameBuilder.append("_");
		int hour = fileOriginalDateCalendar.get(Calendar.HOUR_OF_DAY);
		dirNameBuilder.append(hour < 10 ? "0" + hour: hour).append("h");
		int minutes = fileOriginalDateCalendar.get(Calendar.MINUTE);
		dirNameBuilder.append(minutes < 10 ? "0" + minutes: minutes).append("m");
		int seconds = fileOriginalDateCalendar.get(Calendar.SECOND);
		dirNameBuilder.append(seconds < 10 ? "0" + seconds: seconds).append("s");
		dirNameBuilder.append("_IMG_");
		
		return dirNameBuilder.toString();
	}
	
	public String getFileExtension() {
		return fileExtension;
	}

	public Calendar getFileOriginalDateCalendar() {
		return fileOriginalDateCalendar;
	}


	public File getOriginalFile() {
		return file;
	}
	
	@Override
	public String toString() {

		StringBuilder builder = new StringBuilder();
		
		builder.append("Photo: { ");
		builder.append("OriginalFile: " + file.getAbsolutePath());
		builder.append(" ## TargetFile: " + getBaseFileName() + 1 + getFileExtension());
		builder.append(" }");
		
		return builder.toString();
	}
}
